﻿
var yuiAjaxObject = {
 	JSONParse:function(o) {
		var messages = []; 
		try { 
		    messages = YAHOO.lang.JSON.parse(o.responseText); 
		    return messages;
		} catch (x) { 
		    return null; 
		}
	},
	startRequest:function(method, url, params, success, failure, scope) {
	    var callback = {
	        success:success,
	        failure:failure,
	        scope: scope
	    };
	   YAHOO.util.Connect.asyncRequest(method, url, callback, params);
	}
};


CKEDITOR.plugins.add( 'selectImage',
{
	init: function( editor )
	{
		editor.addCommand( 'selectImageDialog', new CKEDITOR.dialogCommand( 'selectImageDialog' ) );
		
		editor.ui.addButton( 'SelectImage', {
	        label: 'Insert an image by select it.',
	        command: 'selectImageDialog',
	        icon: this.path + 'images/selectimage.gif'
        });
	}
});

CKEDITOR.dialog.add( 'selectImageDialog', function( editor )
{
    return {
		title : 'Select an image to insert',
		minWidth : 600,
		minHeight : 300,
		contents :
		[
			{
				id : 'general',
				label : 'Settings',
				elements :
				[
					{
						type : 'html',
						html : 'This dialog window lets you create simple links for your website.'		
					},
					{
						type : 'text',
						id : 'url',
						label : 'URL',
						validate : CKEDITOR.dialog.validate.notEmpty( 'The link must have a URL.' ),
						required : true,
						commit : function( data )
						{
							data.url = this.getValue();
						}
					},
					{
						type : 'text',
						id : 'title',
						label : 'Title',
						required : false,
						commit : function( data )
						{
							data.title = this.getValue();
						}
					},
					{
						type : 'html',
						id : 'imgc',
						html : '<div id="' + editor.id + '_imgc" style="width: 580px; height: 300px; overflow: auto;"><div>'
					}
				]
			}
		],onShow : function(o)
        {
	        document[this.getParentEditor().id] = this.getParentEditor();
	        
	        var callback = {
                success:function(o) {
                    var dialog = this;
                    
	                var imgCont = document.getElementById(dialog.getParentEditor().id + '_imgc');
	                document[dialog.getParentEditor().id + '_imgc'] = imgCont;
        	        
                    imgCont.innerHTML = '';
                    
                    var result = yuiAjaxObject.JSONParse(o);
                    if (result && result.images) {
                        for (var i = 0; i < result.images.length; i++) {
                            var imgSrc = result.images[i].src;
                            
                            var imgi = document.createElement( 'img' );
	                        imgi.id = imgCont.id + '_img' + (i+1);
	                        imgi.src = imgSrc;
	                        imgi.style.width = "100px";
	                        imgi.style.maxWidth = "100px";
	                        imgi.style.maxHeight = "100px";
	                        imgi.style.cursor = 'pointer';
	                        imgi.alt = imgSrc;
            	            
	                        var linki = document.createElement( 'a' );
	                        linki.id = imgCont.id + '_a' + (i+1);
	                        linki.appendChild(imgi);
	                        linki.style.display = 'block';
	                        linki.style.width = "100px";
	                        linki.style.marginTop = "5px";
	                        linki.style.marginRight = "5px";
	                        linki.style.height = "100px";
	                        linki.style.overflow = "hidden";
	                        linki.style.cssFloat = 'left';
	                        linki.style.cursor = 'pointer';
	                        linki.parentDialog = dialog;
	                        linki.parentEditor = dialog.getParentEditor();
	                        linki.title = imgSrc;
	                        linki.onclick = function() {
	                            this.parentDialog.getContentElement('general', 'url').setValue(this.firstChild.src);
	                            this.parentDialog.click('ok');
	                        };
            	            
	                        imgCont.appendChild(linki);
                        }
                    }
                },
                failure:function(o) {
                }
            };
	        
	        yuiAjaxObject.startRequest('GET', 'ImgListProvider.aspx', 'id='+this.getParentEditor().id, callback.success, callback.failure, this);
        },
		onOk : function()
		{
			var dialog = this,
				data = {},
				img = editor.document.createElement( 'img' );
			this.commitContent( data );

			img.setAttribute( 'src', data.url );

			if ( data.title )
				img.setAttribute( 'title', data.title );

			editor.insertElement( img );
		}
	};
});